const axios = require('axios')
const _ = require('lodash')

let search = async (songName = 'iu') => {
  let url = `http://songsearch.kugou.com/song_search_v2?keyword=${songName}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1489023388641%27`
  url = encodeURI(url)
  let { data } = await axios({
    method: 'get',
    url
  })
  if (data.status == 1) {
    let { data: { lists } } = data
    let newLists = lists.map((value, index, arr) => {
      value.SingerName = value.SingerName.replace('<em>', '').replace('</em>', '')
      value.FileName = value.FileName.replace('<em>', '').replace('</em>', '')
      value = _.pick(value, ['AlbumID', 'FileName', 'FileHash', 'SQFileHash', 'HQFileHash', 'SingerName', 'SongName'])
      return value
    })
    return newLists
  } else {
    return {
      msg: '请求失败'
    }
  }
}

let songData = async (songName = 'iu') => {
  let url = `http://songsearch.kugou.com/song_search_v2?keyword=${songName}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1489023388641%27`
  url = encodeURI(url)
  let { data } = await axios({
    method: 'get',
    url
  })
  return data
}

let downLink = async (id, hash) => {
  let url = `https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=${hash}&album_id=${id}&dfid=3LJRxt3nkMC70zhqjN1lT3C8&mid=ec7ca11000bcdd545906452784cb4832&platid=4&_`
  let {data} = await axios({
    method:'get',
    url
  })
  return data
}




module.exports = {
  search,
  downLink,
  songData
}